<?php

namespace app\index\controller;

use think\Request;
use think\Db;
use Vectorface\Whip\Whip;

class Index extends Common
{
    public function _initialize()
    {
        $this->assign('appName', getenv('APP_NAME'));
        $this->assign('appSite', url('/', '', '', true));
    }

    public function index(Request $request)
    {
        $config = Db::name('config_system')->where(['category' => ['in', ['xitong', 'version']]])->column('title, content');

        if ($request->param('yq')) {
            $whip = new Whip();
            $clientAddress = $whip->getValidIpAddress();

            $result = Db::name('proxy_visit_records')->where(['user_id' => $request->param('yq'), 'ip' => $clientAddress])->find();

            if (!$result) {
                Db::name('proxy_visit_records')->insert([
                    'user_id' => $request->param('yq'),
                    'ip' => $clientAddress,
                    'create_time' => time()
                ]);
            } else {
                Db::name('proxy_visit_records')->where(['id' => $result['id']])->setField('create_time', time());
            }

            $download = Db::name('proxy_download')->where(['user_id' => $request->param('yq')])->order('id desc')->find();

            if ($download && $download['url']) {
                $config['anzhuourl'] = $download['url'];
            }
        }

        $this->assign('config', $config);

        return $this->fetch();
    }

    public function page($page)
    {
        return $this->fetch("page$page");
    }
}
